/****************************************************************************
**
** Copyright (C) 2013 Laszlo Papp <lpapp@kde.org>
** Contact: http://www.qt.io/licensing/
**
** This file is part of the QtSerialPort module of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL21$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see http://www.qt.io/terms-conditions. For further
** information use the contact form at http://www.qt.io/contact-us.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 or version 3 as published by the Free
** Software Foundation and appearing in the file LICENSE.LGPLv21 and
** LICENSE.LGPLv3 included in the packaging of this file. Please review the
** following information to ensure the GNU Lesser General Public License
** requirements will be met: https://www.gnu.org/licenses/lgpl.html and
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** As a special exception, The Qt Company gives you certain additional
** rights. These rights are described in The Qt Company LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** $QT_END_LICENSE$
**
****************************************************************************/

#include "serialportreader.h"

#include <QtSerialPort/QSerialPort>

#include <QCoreApplication>
#include <QFile>
#include <QStringList>

#include <unordered_map>
#include <string>
#include <iostream>
#include <iomanip>



#include <QCoreApplication>
#include <QtSerialPort>
#include <QList>
#include <QDebug>

#include "common.h"
#include "utils.h"




using namespace PCSG;



int main(int argc, char *argv[])
{
    if(argc > 1)
    {
        EnableDisp = true;
    }
    else
    {
        EnableDisp = false;
    }
    /*
    if(argc <= 1)
    {
        std::cout << "Args not enough\n";
        return PCSGError;
    }
    else if( argc % 2 == 0)
    {
        std::cout << "Args not enough\n";
        return PCSGError;
    }

    ///
    /// \brief load params
    ///
    std::string key;
    std::string val;
    std::unordered_map<std::string, std::string> map_args;
    for (int i = 1; i < argc; ++i)
    {
        key = argv[i];
        val = argv[i + 1];
        if(key.find("-") != std::string::npos)
        {
             map_args[key] = val;
        }
        else
        {
            std::cout << "Unknow arg: " << std::left << std::setw(8) << key <<
                         "value: " << val << "\n";
        }
        i++;
    }

    std::cout << "Loaded params:\n";
    std::unordered_map<std::string, std::string>::iterator iter;
    for(iter = map_args.begin(); iter != map_args.end(); iter++)
    {
        std::cout << "Arg: " << std::left << std::setw(8) << iter->first <<
                     "value: " << iter->second << "\n";
    }


    ///
    /// \brief check input params
    ///
    if(map_args.find("-port") == map_args.end())
    {
        std::cout << "Arg missing, -port arg not found \n";
        return PCSGError;
    }
    int baud_rate = QSerialPort::Baud9600;
    if(map_args.find("-rate") != map_args.end())
    {
        baud_rate = atoi(map_args["-rate"].c_str());
        if(baud_rate == 0)
        {
            std::cout << "Invalid baud rate: " << map_args["-rate"] << "\n";
            return PCSGError;
        }
    }*/



    QCoreApplication coreApplication(argc, argv);


    ///
    /// \brief get info of all the serial ports
    ///
    /*
    QList<QSerialPortInfo> list = QSerialPortInfo::availablePorts();
    qout << "Total number of availiable ports:" << list.count() << endl;
    foreach(const QSerialPortInfo &serialportinfo, list)
    {
        qout << "Port: " << serialportinfo.portName() << endl;
        qout << "Location: " << serialportinfo.systemLocation() << endl;
        qout << "Description: " << serialportinfo.description() << endl;
        qout << "Manufactutor: " << serialportinfo.manufacturer() << endl;
        qout << "Vendor Indentifier: " << serialportinfo.vendorIdentifier() << endl;
        qout << "Busy: " << serialportinfo.isBusy() << endl;
    }*/

    ///
    /// \brief check folder and file
    ///
    QString folder_base = QCoreApplication::applicationDirPath();
    QString folder_settings = "/settings";
    QString folder_data = "/data";// auto-gened file name
    QString folder_log = "/log";// auto-gened file name



    ///
    /// \brief open serial port and read
    ///
    SerialPortReader serial_port_reader;
    serial_port_reader.init(folder_base, folder_settings, folder_data, folder_log);
    if(!serial_port_reader.open())
    {
        return PCSGError;
    }


    return coreApplication.exec();
}





